﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mitasoft.ERS.Web.Utils;

namespace mitasoft.ERS.Web
{
    public partial class SubmitProblem : System.Web.UI.Page
    {

        #region fields
        private bool ShowDialog;
        #endregion

        #region properties
        #endregion

        #region handlers

        protected void Page_Init(object sender, EventArgs e)
        {
            BindData();

            btnSendProblem2.Click += new EventHandler(btnSend_ServerClick);
            btnClear.Click += new EventHandler(btnClear_Click);
        }

        private void BindData()
        {
            ddlProjects.DataSource = CacheFactory.GetProjects;
            ddlProjects.DataBind();

            ddlType.DataSource = CacheFactory.GetIssueTypeList;
            ddlType.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        void btnSend_ServerClick(object sender, EventArgs e)
        {
            string htmlMessage = hiddenControl.Value;
            
            string title = problemTitle.Value;

            int project = Convert.ToInt32(ddlProjects.SelectedValue);
            int type = Convert.ToInt32(ddlType.SelectedValue);

            SaveToDB(title, project, type, htmlMessage);
        }
        void btnClear_Click(object sender, EventArgs e)
        {
            ClearControls();
        }
        #endregion

        #region override methods

        protected override void OnPreRender(EventArgs e)
        {
            if (Page.IsPostBack)
            {
                if (!Page.ClientScript.IsStartupScriptRegistered("showDialog") && ShowDialog == true)
                {
                    Page.ClientScript.RegisterStartupScript(
                        typeof(SubmitProblem),
                        "ShowDialog",
                        "$(document).ready(function () { $('#dialog').dialog('open'); });",
                        true);
                }
            }

            base.OnPreRender(e);
        }
        #endregion

        #region public methods
        #endregion

        #region private methods

        private void SaveToDB(string title, int project, int type, string htmlMessage)
        {
            try
            {
                using (Business.ERSEntities ctx = new Business.ERSEntities())
                {
                    ctx.InsertIssueFromSubmit(title, htmlMessage, type, project);
                    ctx.SaveChanges();
                }

                ClearControls();
                ShowDialog = !ShowDialog;
            }
            catch (Exception ex)
            {
                throw ex;
                //TODO: Log exception
            }
        }

        private void ClearControls()
        {
            problemTitle.Value = "";
            ddlProjects.ClearSelection();
            ddlType.ClearSelection();
        }
        #endregion
    }
}